package cn.dao.impl;

import cn.dao.BaseDao;
import cn.dao.HouseDao;
import cn.entity.House;
import org.hibernate.Query;

import java.util.Date;
import java.util.List;

public class HouseDaoImpl extends BaseDao implements HouseDao {

    @Override
    public List<House> findHouses(Double price, String username, Date pubDate) {
        StringBuilder hqlsb = new StringBuilder("select house from House house where 1=1 ");
        if (null!=price&&price!=0){
            hqlsb.append(" and price < ?");
        }
        if (null!=username&&username.length()!=0){
            hqlsb.append(" and user_id = (select id from User where name=?)");
        }
        if (null!=pubDate){
            hqlsb.append(" and pubDate >= ?");
        }
        Query query = currentSession().createQuery(hqlsb.toString());
        if (null!=price&&price!=0){
            query.setDouble(0,price);
        }
        if (null!=username&&username.length()!=0){
            query.setString(1,username);
        }
        if (null!=pubDate){
            query.setDate(2,pubDate);
        }
        return query.list();
    }

    @Override
    public List<House> findByTitleAndFloo(String title, Integer floorage) {
        String hql = "from House house where title like ? and floorage >?";
        Query query = currentSession().createQuery(hql);
        query.setString(0,"%"+title+"%");
        query.setInteger(1,floorage);
        return query.list();
    }
}
